package com.hailou.service;

import java.util.Map;

import com.hailou.base.bean.MenuAndCode;
import com.hailou.dao.ContentDao;
import com.hailou.dao.UserDao;
import com.hailou.util.PatternUtil;

/**
 * 和用户菜单有关的业务类
 * @author hailou
 * @date 2013-12-9
 */
public class MenuService {

	public static SinaWeatherReportService swrs = new SinaWeatherReportService();
	public static WeatherReportService wrs = new WeatherReportService();
	public static DomainService gds = new DomainService();
	public static JokeService js = new JokeService();
	public static ContactsService cs = new ContactsService();
	public static TodayInHistoryService ths = new TodayInHistoryService();
	public static BlogForXshellService bxs = new BlogForXshellService();
	public static TicketService ts = new TicketService();
	public static SimsimiService ss = new SimsimiService();
	
	/****************用户选择功能菜单或者关键字，反回文本信息输入后的结果字符串******************************/
	public static String respContentMethod(String fromUserName , String toUserName,String msgType , String content){
		String respContent = "";
		if(content!=null||!"".equals(content)){					 
			if(PatternUtil.isNumber(content.trim())){
				if(!isMainMenu(fromUserName)){
					return respContent = "您目前所处位置是二级菜单，输入主菜单字符无效。"+MenuAndCode.RETURN_STRING;
				}
				Integer flag = Integer.parseInt(content.trim());
				switch(flag){
					case 1:
						UserDao.setFlag1Value(fromUserName, "1");
						respContent="请输入您要查询城市的名称，如【西安】。"+MenuAndCode.RETURN_STRING;
						break;
					case 2:
						UserDao.setFlag1Value(fromUserName, "2");
						respContent="请回复【笑话】二字。"+MenuAndCode.RETURN_STRING;break;
					case 3:
						UserDao.setFlag1Value(fromUserName, "3");
						respContent="请输入您要查询的人名+暗号，如【张三+ab】，为安全请致电作者询问暗号信息。"+MenuAndCode.RETURN_STRING;break;
					case 4:
						UserDao.setFlag1Value(fromUserName, "4");
						respContent="请输入您要查询的域名，不要带www。"+MenuAndCode.RETURN_STRING;break;
					case 5:
						UserDao.setFlag1Value(fromUserName, "5");
						respContent=ths.getTodayInHistoryInfo();break;
					case 6:
						UserDao.setFlag1Value(fromUserName, "6");
						respContent=ts.getTicketMessage();break;
					case 7:
						UserDao.setFlag1Value(fromUserName, "7");
						respContent=bxs.getXshellContentList();break;
					case 9:
						UserDao.setFlag1Value(fromUserName, "9");
						respContent="我是机器人小Q，欢迎和我聊天！";break;
					default:respContent=MenuAndCode.getMainMenu();
						
				}
			}else if("*".equals(content)||"#".equals(content)){
				respContent =  returnMenu(fromUserName,content);
			}else if("?".equals(content)){
				if(!isMainMenu(fromUserName)){
					respContent = "您目前所处位置是二级菜单。"+MenuAndCode.RETURN_STRING;
				}else{
					respContent=MenuAndCode.getMainMenu();
				}
			}else if(MenuAndCode.WEATHER_CODE.equals(UserDao.selectUserMessage(fromUserName).get("flag1"))){
				//respContent = wrs.getWeatherReport(content);
				if(!PatternUtil.isChar(content)&&swrs.getCityCode(content)!=null){
					respContent = swrs.getSinaWeatherReport(content, "a")+MenuAndCode.getWeatherMeun();
				}else if(content.length()==1&&PatternUtil.isChar(content)){
					String cityName = ContentDao.getMaxContentByUserId(fromUserName, " o.content  REGEXP '[^a-zA-Z0-9*&?#]' ");
					respContent = swrs.getSinaWeatherReport(cityName, content);
				}else{
					respContent = returnMenu(fromUserName,content); 
				}
			}else if("笑话".equals(content)&&MenuAndCode.JOKE_CODE.equals(UserDao.selectUserMessage(fromUserName).get("flag1"))){
				respContent = js.getJokeString();
			}else if(cs.isCheckContacts(content)&&MenuAndCode.PHONE_CODE.equals(UserDao.selectUserMessage(fromUserName).get("flag1"))){
				if(cs.getContacts(content).getPhone1()==null||"".equals(cs.getContacts(content).getPhone1())){
					respContent = "您的暗号输入为空或者不正解，请重新输入";
				}else{
					respContent = cs.getContacts(content).getName()+"\n"+"电话："+cs.getContacts(content).getPhone1();
				}
			}else if(content.split("\\.").length==2&&MenuAndCode.DOMAIN_CODE.equals(UserDao.selectUserMessage(fromUserName).get("flag1"))){
				respContent = gds.getDomain(content);
			}else if(MenuAndCode.SIMSIMI_CODE.equals(UserDao.selectUserMessage(fromUserName).get("flag1"))){
				respContent = ss.getSimsimiString(content);
			}else{
				respContent=MenuAndCode.getMainMenu();
			}
		}
		return respContent;
	}
	
	/****************输入“*---#”号返回上一级菜单或者返回主菜单***/
	public static String  returnMenu(String userId , String conten){
		String  respConten = "";
		if ("#".equals(conten)) {
			respConten = MenuAndCode.getMainMenu();
			UserDao.setFlag1AndFlag2Value(userId,new String[]{"0","0"});
		}else if("*".equals(conten)){
			Map<String,String> flag = UserDao.selectUserMessage(userId);
			/********判断用户在一级菜单下 **/
			if(!flag.get("flag1").equals("0")&&flag.get("flag2").equals("0")){
				respConten = MenuAndCode.getMainMenu();
				UserDao.setFlag1AndFlag2Value(userId,new String[]{"0","0"});
			}else{
				//目录没有进入二级菜单需要返回的，这部分功能未开发
				/********修改这里可以返回用户的菜单的树节点 
				Map<String, String> map = new LinkedHashMap<String, String>();
				map.put("flag2", "0");
				UserDao.updateUserMessage(userId,map);
				**/
			}
		}
		return respConten;
	}
	/****************判断是否在主菜单下****************/
	public static  boolean isMainMenu(String fromUserName){
		boolean flag = false;
		Map<String,String> map = UserDao.selectUserMessage(fromUserName);
		String flag1 = map.get("flag1");
		if("0".equals(flag1)){
			flag = true;
		}
		return flag;
	}
}
